package xyz.penghaonan.p2plib.job.jobs;

import android.net.wifi.p2p.WifiP2pDevice;

import com.penghaonan.appframework.utils.WeakObservableList;

import java.util.Collection;

import xyz.penghaonan.p2plib.job.base.BaseJob;
import xyz.penghaonan.p2plib.job.base.JobListener;
import xyz.penghaonan.p2plib.job.base.Operation;
import xyz.penghaonan.p2plib.job.base.OperationListener;
import xyz.penghaonan.p2plib.job.base.OperationListenerAdapter;
import xyz.penghaonan.p2plib.job.base.OperationListenerForJob;
import xyz.penghaonan.p2plib.job.operations.ConnectServerOperation;
import xyz.penghaonan.p2plib.job.operations.ScanServerOperation;
import xyz.penghaonan.p2plib.job.operations.StartServiceOperation;

public class StartClientJob extends BaseJob {

    public StartClientJob() {
        StartServiceOperation startServiceOperation = new StartServiceOperation();
        startServiceOperation.addListener(new OperationListenerForJob() {

            @Override
            public void onOperationFinish(Operation operation, final Operation.Result result) {
                if (result.success) {
                    doCreateGroupOperation();
                } else {
                    jobListenerList.iterate(new WeakObservableList.OnIterator<JobListener>() {
                        @Override
                        protected boolean onIterator(JobListener item) {
                            item.onJobFinish(false, result.msg);
                            return false;
                        }
                    });
                }
            }
        });

        setHeaderOperation(startServiceOperation);
    }

    private void doCreateGroupOperation() {
        ScanServerOperation operation = new ScanServerOperation();
        operation.addListener(new OperationListenerForJob<Collection<WifiP2pDevice>>() {

            @Override
            public void onOperationFinish(Operation<Collection<WifiP2pDevice>> operation, Operation.Result<Collection<WifiP2pDevice>> result) {
                if (result.success) {
                    WifiP2pDevice device = result.data.iterator().next();
                    if (device == null) {
                        notifyJobFinish(false, "No server found!");
                    } else {
                        doConnectServerOperation(device);
                    }
                } else {
                    notifyJobFinish(false, result.msg);
                }
            }
        });
        executeOperation(operation);
    }

    private void doConnectServerOperation(WifiP2pDevice wifiP2pDevice) {
        ConnectServerOperation operation = new ConnectServerOperation();
        operation.addListener(new OperationListenerForJob() {
            @Override
            public void onOperationFinish(Operation operation, Operation.Result result) {
                super.onOperationFinish(operation, result);
                notifyJobFinish(result.success, result.msg);
            }
        });
        operation.setWifiP2pDevice(wifiP2pDevice);
        executeOperation(operation);
    }
}
